package leetcode.code1262;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

//执行用时：6 ms, 在所有 Java 提交中击败了82.91%的用户
//
//内存消耗：47.9 MB, 在所有 Java 提交中击败了5.03%的用户
//
//通过测试用例：40 / 40
public class Solution implements IDebug {

	public int maxSumDivThree(int[] nums) {
		int mod0 = 0, mod1 = 0, mod2 = 0;
		int cur0 = 0, cur1 = 0, cur2 = 0;
		for (int num : nums) {
			int mod = num % 3;
			if (mod == 0) {
				cur0 = mod0 + num;
				cur1 = mod1 == 0 ? mod1 : mod1 + num;
				cur2 = mod2 == 0 ? mod2 : mod2 + num;
			} else if (mod == 1) {
				cur0 = mod2 == 0 ? mod0 : Math.max(mod0, mod2 + num);
				cur1 = Math.max(mod1, mod0 + num);
				cur2 = mod1 == 0 ? mod2 : Math.max(mod1, mod1 + num);
			} else if (mod == 2) {
				cur0 = mod1 == 0 ? mod0 : Math.max(mod0, mod1 + num);
				cur1 = mod2 == 0 ? mod1 : Math.max(mod1, mod2 + num);
				cur2 = Math.max(mod2, mod0 + num);
			}
			mod0 = cur0;
			mod1 = cur1;
			mod2 = cur2;
		}
		return mod0;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(18, this.maxSumDivThree(HelpDebug.str2array3("[3,6,5,1,8]")));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub
		HelpDebug.compare(12, this.maxSumDivThree(HelpDebug.str2array3("[1,2,3,4,4]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub
		HelpDebug.compare(0, this.maxSumDivThree(HelpDebug.str2array3("[4]")));

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub
		HelpDebug.compare(9, this.maxSumDivThree(HelpDebug.str2array3("[5,2,2,2]")));
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug4();
		so.debug1();
		so.debug2();
		so.debug3();

	}

}
